#!/usr/bin/env bash
# FOSSology mod_deps script
# This script helps you install dependencies on a system. for a module

# SPDX-FileCopyrightText: © 2011-2014 Hewlett-Packard Development Company, L.P.

# SPDX-License-Identifier: GPL-2.0-only

source "$(dirname ${BASH_SOURCE[0]})/../../utils/utils.sh"

#
# Don't show the -y option.  Should only be used for install testing, as using
# it without being careful can destroy your system.
#
YesOpt=''

EVERYTHING=''
RUNTIME=''
BUILDTIME=''

## Options parsing and setup
# parse options
OPTS=$(getopt -o rbehy --long runtime,buildtime,everything,help -n 'mod_deps' -- "$@")

if [[ $? -ne 0 ]]; then
  OPTS='--help'
fi

eval set -- "$OPTS"

# if no options or just -y then do everything
if [[ $OPTS == ' --' || $OPTS == ' -y --' ]]; then
  EVERYTHING=true
fi

while true; do
  case "$1" in
    -r|--runtime)     RUNTIME=true; shift;;
    -b|--buildtime)   BUILDTIME=true; shift;;
    -e|--everything)  EVERYTHING=true; shift;;
    -y)               YesOpt='-y'; shift;;
    -h|--help)        show_help_for_mod_deps; exit;;
    --)               shift; break;;
    *)                echo "ERROR: option $1 not recognised"; exit 1;;
  esac
done

set -o errexit -o nounset -o pipefail

must_run_as_root
need_lsb_release

if [[ $EVERYTHING ]]; then
  echo "*** Installing both runtime and buildtime dependencies ***"
  RUNTIME=true
  BUILDTIME=true
fi

# figure out what distro we're on
DISTRO=$(lsb_release --id --short)

########################################################################

if [ $BUILDTIME ]; then
  echo "*** Installing $DISTRO buildtime dependencies ***";
  case "$DISTRO" in
    Debian|Ubuntu)
      apt-get $YesOpt install \
        libgcrypt20-dev
      ;;
    RedHatEnterprise*|CentOS|Fedora)
      yum $YesOpt install \
        libgcrypt-devel
      ;;
  esac
fi

if [ $RUNTIME ]; then
  echo "*** Installing $DISTRO runtime dependencies ***";
  case "$DISTRO" in
    Debian|Ubuntu)
      apt-get $YesOpt install \
        bzip2 cabextract cpio genisoimage p7zip poppler-utils rpm tar \
        unzip gzip sleuthkit p7zip-full unrar-free libgcrypt20 zstd
      ;;
    RedHatEnterprise*|CentOS)
      yum $YesOpt install \
        bzip2 cpio mkisofs p7zip poppler-utils rpm tar unzip gzip p7zip-plugins\
        sleuthkit unrar libgcrypt zstd
      ;;
    Fedora)
      yum $YesOpt install \
        bzip2 cabextract cpio genisoimage p7zip p7zip-plugins poppler-utils rpm\
        tar unzip gzip sleuthkit unrar libgcrypt zstd
      ;;
  esac
fi

#######################################################################
